package cloudspace.vm.io.console;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;


/**
 * This is the comet update thread. It constantly queries the Console looking
 * for updates to any of the registered streams. If an update is detected the
 * parent Component is notified.
 */
public class CometExecutor
{

    private static final int POOL_SIZE = 100;

    private static final ScheduledExecutorService pool = Executors.newScheduledThreadPool( POOL_SIZE );


    private CometExecutor()
    {
    }


    /**
     * Adds a listener to the CometUpdateThread. This listener will be checked
     * during the normal run sequence.
     * 
     * @param cel
     *            the event listener to add to the comet thread.
     */
    @SuppressWarnings("unchecked")
    public static void addListener( CometEvent cel )
    {
        ScheduledFuture<CometEvent> future = (ScheduledFuture<CometEvent>)pool.scheduleWithFixedDelay( cel,
            0,
            1,
            TimeUnit.SECONDS );
        cel.giveHandle( future );
    }


    public static void shutdown()
    {
        pool.shutdownNow();
    }

}
